<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/18 0018
 * Time: 上午 9:41
 */

namespace backend\controllers;

use backend\components\BaseComponentController;
use common\components\ActionsComponents\UploadLocalAction;
use common\components\ActionsComponents\UploadOssAction;
use common\components\DataHelper;
use common\components\GlobalUrlComponent;
use common\models\Gift\Gift;
use common\models\Goods\Goods;
use common\models\MchUpic\MchUpic;
use common\models\Merchant\Merchant;
use yii\helpers\Url;

class AttachmentController extends BaseComponentController
{
    //这里就不需要csr验证了 因为是统一的附件上传
    public $enableCsrfValidation = false;

    public function actions()
    {
        $isYmd = $this->post('ymd', true); //生成 ymd目录
        return [
            'upload' => [
                'class' => UploadOssAction::class,
//                'isBase64' => $this->_request('is_base64', true),
                'isBase64' => $this->requestAll('is_base64') == -1 ? false : true,
                'uploadName' => 'image',
                'filename' => $this->requestAll('filename', null), //指定生成文件名
                'dir' => "upload/" . trim($this->get('upload_dir', 'attachment'), '/') . '/' . ($isYmd?date("Y-m-d") . '/':''),
                'callback' => function($imageRet) {
                    return true;
                },
                'size' => 10,
             //   'suffix' => 'jpg'
            ],
            "upload-local" => [
                "class" => UploadLocalAction::class,
                'uploadName' => 'image',
                "isBase64" => $this->requestAll('is_base64', true),
                'filename' => $this->requestAll('filename', null), //指定生成文件名
                "saveAs" => "upload/" . trim($this->requestAll('upload_dir', 'attachment'), '/') . '/' . ($isYmd?date("Y-m-d") . '/':''),/*[
                    "app" => "backend",
                    "path" => trim($this->requestAll('upload_dir', 'attachment'), '/') . '/' . ($isYmd?date("Y-m-d") . '/':'')
                ]*/
            ],
            'upload-editor' => [
                'class' => UploadLocalAction::class,
                'isEditor' => true,
                'uploadName' => 'upfile',
                'dir' => "upload/" .  trim($this->get('upload_dir', 'attachment'), '/') . '/' . ($isYmd?date("Y-m-d") . '/':''),
            ]
        ];
    }

    public function actionShowImg()
    {
        $this->layout = self::LAYER_CONTENT;
        return $this->render("show-img", [
            "w" => $this->get("w", "100%"),
            "h" => $this->get("h", "100%"),
            "image" => $this->get("url", DataHelper::getDefaultImage())
        ]);
    }

    /**
     * 这里是裁剪上传open页面
     * @return string
     */
    public function actionCropper()
    {
        $this->layout = self::LAYER_CONTENT;
        return $this->render('cropper', [
            'filename'=>$this->get('filename'),
            'callback_input' => $this->get('callback_input', '#file'),
            'callback_view' => $this->get('callback_view', '#view'),
            'upload_dir' => $this->get('upload_dir', 'attachment'),
            'upload_url' => $this->get('upload_url', Url::to(['upload'])),
            'callbackSuccess' => $this->get('callback_success', "")
        ]);
    }

}